import { Body, Controller, Delete, Headers, Post, UseInterceptors } from '@nestjs/common';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { extractToken } from 'src/utils';
import { AuthService } from './auth.service';
import { LoginAuthDto } from './dto/login-auth.dto';
import { Public } from './public.decorator';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Public()
  @Post('login')
  @UseInterceptors(AnyFilesInterceptor())
  // 验证码要用 @UploadedFiles() files: Array<Express.Multer.File>
  async login(@Body() body: LoginAuthDto) {
    return await this.authService.login(body.username, body.password);
  }

  @Delete('logout')
  async logout(@Headers('Authorization') auth: string) {
    const token = extractToken(auth);
    return await this.authService.logout(token);
  }
}
